// pages/andon/gateway_query/gateway_query.js
const mybehavior = require("../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    factory: "",
    factories: [],
    timer: null,
    page_all: 1,
    location: "西区总装",
    columns: [{
      title: '网关编号',
      dataIndex: 'raspberry_num',
      key: 'raspberry_num',
      width: 125,
      align: 'center',
    },
    {
      title: '安装位置',
      dataIndex: 'work',
      key: 'work',
      width: 125,
      align: 'center',
    },
    {
      title: '上次心跳时间',
      dataIndex: 'heart_time',
      key: 'heart_time',
      width: 150,
      align: 'center',
    },
    {
      title: '状态',
      dataIndex: 'state',
      key: 'state',
      width: 80,
      align: 'center',
    },
    ],
    data: [],
    pagination: {
      defaultCurrent: 1,
      hideOnSinglePage: true,
      total: 0,
      current: 1,
      pageSize: 10,
    }
  },
  pageUp() {
    let pagination = this.data.pagination;
    if (pagination.current != 1) {
      pagination.current = pagination.current - 1
      this.setData({
        pagination: pagination,
      })
      this.get_tableData()
    }
  },
  pageDown() {
    let pagination = this.data.pagination
    if (pagination.current < this.data.page_all + 1) {
      pagination.current = pagination.current + 1
      this.setData({
        pagination: pagination,
      })
      this.get_tableData()
    }
  },
  get_tableData() {
    let that = this;
    this.get('http://logisticsengineering.gree.com/andon_system/get_getway_info', {
      'factory': that.data.factory,
      'current': that.data.pagination.current
    }).then((res) => {
      let pagination = that.data.pagination;
      pagination.total = res.data.table_total;
      that.setData({
        page_all: parseInt(that.data.pagination.total / that.data.pagination.pageSize),
        data: res.data.table_data,
        pagination: pagination
      })
    })
  },
  getFactories() {
    const that = this;
    const url = "https://logisticsengineering.gree.com/andon_system/get_factory_list";
    this.get(url).then((res) => {
      let options = []
      for (let i = 0; i < res.data.length; i++) {
        let option = {};
        option.value = res.data[i];
        option.text = res.data[i];
        options.push(option);
      }
      that.setData({
        factories: options
      })
      console.log(res)
    }).catch((err) => {
      console.log(err)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getFactories();
    this.get_tableData();
    const timer = setInterval(this.get_tableData, 5000)
    this.setData({
      timer: timer
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    const res = clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})